package com.sky.web.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageDTO;
import com.sky.result.PageBean;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    //套餐分页查询
    @GetMapping("/page")
    public Result findByPage(SetmealPageDTO setmealPageDTO) {
        PageBean<SetmealVO> pageBean = setmealService.findByPage(setmealPageDTO);
        return Result.success(pageBean);
    }

    //新增套餐
    /*因为当管理端操作数据库修改时,如果不清除缓存,微信小程序端还是查的缓存,不能保持与数据库的一致性,所以需要清除缓存,*/
    @CacheEvict(value = "setmealCache", key = "#setmealDTO.categoryId")//清理指定缓存
    @PostMapping
    public Result save(@RequestBody SetmealDTO setmealDTO) {
        setmealService.save(setmealDTO);
        return Result.success();
    }

    //回显套餐->根据id查询套餐
    @GetMapping("/{id}")
    public Result findByid(@PathVariable Long id) {
        SetmealVO setmealVO = setmealService.findById(id);
        return Result.success(setmealVO);
    }

    //修改套餐
    @CacheEvict(value = "setmealCache", allEntries = true)//清空所有缓存
    @PutMapping
    public Result update(@RequestBody SetmealDTO setmealDTO) {
        setmealService.update(setmealDTO);
        return Result.success();
    }

    //套餐的起售、停售
    @CacheEvict(value = "setmealCache",allEntries = true)//清空所有缓存
    @PostMapping("/status/{status}")
    public Result updateStatus(Long id, @PathVariable Integer status) {
        setmealService.updateStatus(id, status);
        return Result.success();
    }

    //批量删除套餐
    @CacheEvict(value = "setmealCache", allEntries = true)//清空所有缓存
    @DeleteMapping
    public Result deleteByIds(Long[] ids) {
        setmealService.deleteByIds(ids);
        return Result.success();
    }


}
